let gulp=require('gulp');
let cssmin=require('gulp-cssmin');
let jsmin=require('gulp-uglify');
let htmlmin=require('gulp-htmlmin');
let gulpclean = require("gulp-clean");
let webserver=require('gulp-webserver')
function css(){
  return  gulp.src('./src/css/**')
    .pipe(cssmin())
    .pipe(gulp.dest('./dist/css'));
}
function js(){
    return gulp.src('./src/js/**')
    .pipe(jsmin())
    .pipe(gulp.dest('./dist/js'))
}
function html(){
    return gulp.src('./src/html/**')
    .pipe(htmlmin(
          {
        collapseWhitespace: true, // 表示去除空格
        removeEmptyAttributes: true, // 移出空的属性
        minifyCSS: true, // 压缩 style 标签
        minifyJS: true, // 压缩 script 标签
    }
    ))
    .pipe(gulp.dest('./dist/html'))
}
function copy(){
  return gulp.src('./src/images/**').pipe(gulp.dest('./dist/images'))
}
function val(){
  return gulp.src('./ziyuan/**').pipe(gulp.dest('./dist/ziyuan'))
}
function clear(){
    return gulp.src(["./dist"]).pipe(gulpclean());
}

function web(){
      return gulp.src('./dist').pipe(webserver({
        host:'localhost',
        port:3000,
        open:"./html/index.html",
        livereload:true,
          proxies: [
                   
                    {
                        source: "/dl", 
                        target: "https://wx.maoyan.com/mmdb", 
                    },
                     {
                        source: "/self", 
                        target: "http://gz2107/project/html/api",
                    },
                      {
                        source: "/lol", //随便起，相当于你要请求接口的替换名 ajax请求时就用这个名字
                        target: "https://game.gtimg.cn", //需要请求的接口
                    },
                   
                ],
      })
       )
}
// 创建一个实时监听的任务，当监听到某个文件夹中的文件发生改变的时候 自动帮我们执行build文件

function watch() {
    gulp.watch('./src/css', css);
    gulp.watch('./src/html', html);
    gulp.watch('./src/js', js);
    gulp.watch('./src/images', copy);
    gulp.watch('./validation', val);
}
exports.css=css;
exports.js=js;
exports.htm=html;
exports.copy=copy;
exports.clean=clear;
exports.val=val;
// exports.build = gulp.series(clear, css, js, html, copy);
// exports.build=gulp.series(css,js,html,copy);
// exports.build = gulp.series(clear, gulp.parallel(css, js, html, copy));
exports.build = gulp.series(clear, gulp.parallel(css, js, html, copy,val),web,watch);